package com.niubi.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.niubi.yygh.hosp.repository.DepartmentRepository;
import com.niubi.yygh.hosp.service.DepartmentService;
import com.niubi.yygh.model.hosp.Department;
import com.niubi.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

  @Autowired
  DepartmentRepository departmentRepository;

  @Override
  public void saveDept(Map<String, Object> map) {
    String jsonString = JSONObject.toJSONString(map);
    Department department = JSONObject.parseObject(jsonString, Department.class);
    Department departmentExist = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
    if (departmentExist != null){
      department.setId(departmentExist.getId());
      department.setCreateTime(departmentExist.getCreateTime());
      department.setUpdateTime(new Date());
      departmentRepository.save(department);
    }else {
      department.setCreateTime(new Date());
      department.setUpdateTime(new Date());
      departmentRepository.save(department);
    }
  }

  @Override
  public Page<Department> selectPage(int page, int limit) {
    Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
    Pageable pageable = PageRequest.of(page - 1, limit,sort);
    Department department = new Department();
    Example<Department> example = Example.of(department);
    Page<Department> pageModel = departmentRepository.findAll(example,pageable);
    return pageModel;
  }

  @Override
  public void removeDept(String hoscode, String depcode) {
    Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
    if (department !=null){
      departmentRepository.deleteById(department.getId());
    }
  }

  @Override
  public List<DepartmentVo> findDeptTree(String hoscode) {
    Department department = new Department();
    department.setHoscode(hoscode);
    Example<Department> example = Example.of(department);
    List<Department> departmentList = departmentRepository.findAll(example);
    Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
    List<DepartmentVo> result = new ArrayList<>();

    for (Map.Entry<String,List<Department>> entry : departmentMap.entrySet()) {
      String bigcode = entry.getKey();
      List<Department> smallDeptList = entry.getValue();
      DepartmentVo departmentVo = new DepartmentVo();
      departmentVo.setDepcode(bigcode);
      String bigname = smallDeptList.get(0).getBigname();
      departmentVo.setDepname(bigname);
      List<DepartmentVo> children = new ArrayList<>();
      for (Department departmentSmall : smallDeptList) {
        DepartmentVo departmentVoSmall = new DepartmentVo();
        departmentVoSmall.setDepcode(departmentSmall.getDepcode());
        departmentVoSmall.setDepname(departmentSmall.getDepname());
        children.add(departmentVoSmall);
      }
      departmentVo.setChildren(children);
      result.add(departmentVo);
    }
    return result;
  }

  @Override
  public Department getDeptName(String hoscode, String depcode) {
    return departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
  }
}
